﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class PracticeNewPanel : UserControl
    {
        public static event NewPracticeCreatedHandler EventNewPractice;
        PracticeCard pCard;
        public PracticeNewPanel(PracticeCard pCard)
        {
            InitializeComponent();
            this.pCard = pCard;
            this.Dock = DockStyle.Fill;

            EventNewPractice += new NewPracticeCreatedHandler(pCard.NewPracticeEvent);
        }


        private void buttonGo_Click(object sender, EventArgs e)
        {
            //there are 2 fields which are a must.
            if (textBoxLocation.Text.Equals(string.Empty) || textBoxLocation.Text == null || dateTimePickerStart.Value.Year == 2000)
                MessageBox.Show("תאריך התחלה וכן מיקום התעסוקה הם שדות חובה");
            // the must fields are set and valid --->>> create new Tahasuka
            else
            {
                //checks if there a row with this name in TahasukaData Table.
                string selectStr = @"SELECT COUNT(*) FROM [PracticeData] WHERE startDate=@startDate";
                SqlCommand sqlCmd = SqlManager.SqlCommandFactory(selectStr);
                sqlCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = dateTimePickerStart.Value.Date;
                int check_id = SqlManager.executeSqlScalar(sqlCmd);

                //this case occurs when the id is valid --->>> no current open Tahasuka --->>> create new one.
                if (check_id == 0)
                {
                    string insertStr = @"INSERT INTO [PracticeData] (location, startDate,endDate, region, isClosed)"
                                      + "VALUES (@location, @startDate, @endDate, @region, @isClosed)";
                    sqlCmd = SqlManager.SqlCommandFactory(insertStr);

                    sqlCmd.Parameters.Add("@location", SqlDbType.NVarChar).Value = textBoxLocation.Text;
                    sqlCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = dateTimePickerStart.Value;
                    sqlCmd.Parameters.Add("@endDate", SqlDbType.Date).Value = dateTimePickerEnd.Value;
                    sqlCmd.Parameters.Add("@region", SqlDbType.NVarChar).Value = comboBoxRegion.Text;
                    sqlCmd.Parameters.Add("@isClosed", SqlDbType.Bit).Value = false;
                    SqlManager.executeNonQuery(sqlCmd);

                    //fire this event
                    pCard.IsOpen = true;
                    EventNewPractice(this, EventArgs.Empty);
                }
                else MessageBox.Show(".קיים אימון בתאריכים אלו. נא לבחור תאריך פנוי");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            pCard.changeMainPanel(); 
        }

        public override string ToString()
        {
            return "יצירת כרטיס חדש";
        }
    }
}
